'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminTzfwUsersCtrl
 * @description
 * # 用户选择人员页面
 * # 用户需要提供选过的人exclusionUsers字段，用来回显
 * # 数据接口路径由$routeParams.url获取
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('AdministratorAdminTzfwUsersCtrl', function (DataService, Util, $rootScope, openApi, localStorageService, $routeParams) {
    var vm = this;
    this.peoplelist = [];
    var users = localStorageService.get("users") || [];
    this.handleUser = function (userObj) {
      if (userObj.selected) {
        users.push(userObj)
      } else {
        users = users.filter(function (item, index) {
          return item[item.length - 1] != userObj[userObj.length - 1]
        })
      }
    };
    var init = function () {
      DataService.getData(openApi.getUsers).then(function (data) {
        vm.peoplelist = data
        users.forEach(function (item, index) {
          for (var index = 0; index < vm.peoplelist.length; index++) {
            var element = vm.peoplelist[index];
            if (element[element.length - 1] == item[item.length - 1]) {
              element.selected = true;
              return
            }
          }
        })
      })
    }
    this.confirm = function () {
      localStorageService.set("users", users);
      console.log(users);
      Util.goBack();
    }
    init()
  });